package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.KLog;


public class SetBankCardAccountViewModel extends BaseViewModel {
    //真实姓名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    public ObservableField<String> userWangDian = new ObservableField<>("");
    public ObservableField<String> userIDCard = new ObservableField<>("");
    public ObservableField<String> userPhone = new ObservableField<>("");
    //支付宝或者微信账号的绑定
    public ObservableField<String> userAccount = new ObservableField<>("");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt clearAccountBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt clearWangDianBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt clearIDCardBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt clearPhoneBtnVisibility = new ObservableInt(View.INVISIBLE);

    public SetBankCardAccountViewModel(@NonNull Application application) {
        super(application);
    }

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });
    public BindingCommand clearUserAccountOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userAccount.set("");
        }
    });
    public BindingCommand clearUserWangDianOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userWangDian.set("");
        }
    });
    public BindingCommand clearUserIDCardOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userIDCard.set("");
        }
    });
    public BindingCommand clearUserPhoneOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userPhone.set("");
        }
    });
    //用户名长度改变的回调事件
    public BindingCommand onTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearBtnVisibility.set(View.INVISIBLE);
            } else {
                clearBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userName.get())) {
                clearBtnVisibility.set(View.VISIBLE);
            } else {
                clearBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    public BindingCommand onAccountTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearAccountBtnVisibility.set(View.INVISIBLE);
            } else {
                clearAccountBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    public BindingCommand<Boolean> onAccountFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userAccount.get())) {
                clearAccountBtnVisibility.set(View.VISIBLE);
            } else {
                clearAccountBtnVisibility.set(View.INVISIBLE);
            }
        }
    });

    public BindingCommand onIDCardTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearIDCardBtnVisibility.set(View.INVISIBLE);
            } else {
                clearIDCardBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    public BindingCommand<Boolean> onIDCardFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userIDCard.get())) {
                clearIDCardBtnVisibility.set(View.VISIBLE);
            } else {
                clearIDCardBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    public BindingCommand onWangDianTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearWangDianBtnVisibility.set(View.INVISIBLE);
            } else {
                clearWangDianBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    public BindingCommand<Boolean> onWangDianFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userWangDian.get())) {
                clearWangDianBtnVisibility.set(View.VISIBLE);
            } else {
                clearWangDianBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    public BindingCommand onPhoneTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearPhoneBtnVisibility.set(View.INVISIBLE);
            } else {
                clearPhoneBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    public BindingCommand<Boolean> onPhoneFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userPhone.get())) {
                clearPhoneBtnVisibility.set(View.VISIBLE);
            } else {
                clearPhoneBtnVisibility.set(View.INVISIBLE);
            }
        }
    });


    //提交按钮的点击事件
    public BindingCommand confirmOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });


    public void setBundleInfo(Bundle bundle) {

    }
}
